package action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entity.*;

public class ManagerAction extends ActionSupport { 

	/**
	 * 
	 */
	private static final long serialVersionUID = 764122869845232420L;
	@SuppressWarnings("rawtypes")
	private List list = null;
	
	
	private File file;
	private String fileFileName;

	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public ManagerAction(){
		
	}
	
	private PrintWriter out;
	private Session session;
	public void beginOut() throws IOException{
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");    
		out = ServletActionContext.getResponse().getWriter();
		session = HibernateSessionFactory.getSession();session.beginTransaction();
	}
	public void closeOut(){
		out.flush(); out.close();
		session.getTransaction().commit();
		//System.out.println("shut");
		session.close();
	}

	public String newFile(String folder,String filename) throws IOException{
		String path = ServletActionContext.getServletContext().getRealPath(folder);	
		if(file!=null){
			System.out.println(file.getAbsolutePath());
			System.out.println(filename);
			File savefile = new File(new File(path), filename );
			if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
			FileUtils.copyFile(file, savefile);
		}
		return null;
	}
	
	
} 
